#include "Win.h"
#include <tchar.h>

//////////////////////////////////////////////////////////////////
// Static Initialisation
//////////////////////////////////////////////////////////////////
static CWin * g_pCWin		= NULL;
HINSTANCE CWin::m_hInstance = GetModuleHandle(NULL);

//////////////////////////////////////////////////////////////////
// Connectivity WIN32 -> Class
//////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	return g_pCWin->MsgProc(hWnd, uMsg, wParam, lParam);
}

//////////////////////////////////////////////////////////////////
// Constructors/Destructors
//////////////////////////////////////////////////////////////////
CWin::CWin()
{
	g_pCWin		 = this;

	this->m_hWnd = NULL;
	this->m_dwCreationFlags  = 0L;
	this->m_dwWindowStyle	= WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX;
	this->m_dwExWindowStyle	= WS_EX_STATICEDGE;
	this->m_dwCreationFlags	= SW_SHOW;
	this->m_PosX			= CW_USEDEFAULT;	
	this->m_PosY			= CW_USEDEFAULT;	
	this->m_dwCreationWidth	= 800;
	this->m_dwCreationHeight	= 600;
	this->m_hbrWindowColor	= (HBRUSH)(COLOR_WINDOW+1);
	this->m_hIcon			= (HICON)LoadImage(NULL, "res/macos.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE); //LoadIcon(m_hInstance, (LPCTSTR)IDI_APPLICATION);
	this->m_strWindowTitle	= _T("Brocko VS Mutant Fruit~!");
	this->m_hMenu			= NULL; 	
}

CWin::~CWin()
{
}


//////////////////////////////////////////////////////////////////
// Functions
//////////////////////////////////////////////////////////////////
int CWin::Run()
{
	MSG msg;

	ZeroMemory( &msg, sizeof(msg) );
	GameInit();
	
	// Get the current time.
	//float lastTime = (float)timeGetTime();

	while( msg.message!=WM_QUIT )
	{
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
			//GameLoop();

		} 
		else 
		{
			// Get the time now.
		//	float currTime = (float)timeGetTime();
			// Compute the differences in time from the last
			// time we checked. Since the last time we checked
			// was the previous loop iteration, this difference
			// gives us the time between loop iterations...
			// or, I.e., the time between frames.
	//		float deltaTime = (currTime - lastTime)*0.001f;

	//		DrawFramesPerSecond(deltaTime);
			GameLoop();

			// We are at the end of the loop iteration, so
			// prepare for the next loop iteration by making
			// the "current time" the "last time."
	//		lastTime = currTime;
		}
	}
	
	GameEnd();
	
	return msg.wParam;
}

HRESULT CWin::Create()
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style		= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra	= 0;
	wcex.cbWndExtra	= 0;
	wcex.hInstance		= m_hInstance;
	wcex.hIcon		= m_hIcon;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= m_hbrWindowColor;
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= _T("Brocko");
	wcex.hIconSm		= NULL;

	::RegisterClassEx(&wcex);

	m_hWnd = ::CreateWindowEx(m_dwExWindowStyle,_T("Brocko"), m_strWindowTitle, m_dwWindowStyle,
	  m_PosX, m_PosY, m_dwCreationWidth, m_dwCreationHeight, NULL, m_hMenu, m_hInstance, NULL);

	if (!m_hWnd)
	{
	  return FALSE;
	}

	::ShowWindow(m_hWnd, m_dwCreationFlags);
	::UpdateWindow(m_hWnd);

	return TRUE;

}

LRESULT CWin::MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	int wmId;
	int wmEvent;

	if (!m_hWnd)
		m_hWnd = hWnd;

	switch (uMsg) 
	{
		case WM_CREATE:
			graphics = ::GetDC(hWnd);
		break;
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			break;
		case WM_ERASEBKGND:
			return 1;
		break;
		case WM_PAINT:
			GameLoop();
		case WM_KEYDOWN:
			switch(wParam){
				case VK_LEFT:
					input.PressLeft();
					break;
				case VK_RIGHT:
					input.PressRight();
					break;
				case VK_UP:
					input.PressUp();
					break;
				case VK_DOWN:
					input.PressDown();
					break;
				case VK_ESCAPE:
					input.PressEscape();
					break;
				case VK_RETURN:
					input.PressEnter();
					break;
				case VK_F1:
					input.PressF1();
					break;
				default:
					break;
			}
			break;
		case WM_KEYUP:
			switch(wParam){
				case VK_LEFT:
					input.ReleaseLeft();
					break;
				case VK_RIGHT:
					input.ReleaseRight();
					break;
				case VK_UP:
					input.ReleaseUp();
					break;
				case VK_DOWN:
					input.ReleaseDown();
					break;
				case VK_RETURN:
					input.ReleaseEnter();
					break;
				case VK_F1:
					input.ReleaseF1();
					break;
			}

			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, uMsg, wParam, lParam);
   }
   return 0;
}